<!-- @format -->

<!--添加修改用户信息-->
<template>
    <a-modal v-model="showAddTable" :width="640"
             title="出厂设置"
             :maskClosable="false"
             :destroyOnClose="true" @cancel="cancelApply">
        <a-form :form="form" @keydown.native.enter.prevent="saveApply" class="modelSearch">
            <a-form-item v-bind="formStyle" label="设备序列号" required>
                <a-input
                        placeholder="请输入设备序列号"
                        v-decorator="[
                        'deviceSerialNumber',
                        {
                            rules: [
                                { required: true, message: '请输入设备序列号' },
                                { validator: this.verify.valNum2.bind(this) }
                            ],
                            getValueFromEvent: event => {
                                return this.verify.formalSettingSpace(event,32)
                            }
                        }
                    ]"
                />
            </a-form-item>
           <!-- <a-form-item label="证书连接方式：" class="handFile" v-bind="formStyle" required style="position: relative">
                <a-select :getPopupContainer="trigger => trigger.parentNode"
                          placeholder="请选择证书连接方式"
                          v-decorator="['subMod', { initialValue: '0' }, { rules: [{ required: true, message: '请选择证书连接方式' }] }]">
                    <a-select-option v-for="item in subModeList" :value="item.value" :key="item.value">{{
                        item.label }}
                    </a-select-option>
                </a-select>
            </a-form-item>
            <a-form-item v-bind="formStyle" label="证书主题" required>
                <a-input
                        placeholder="请输入证书主题"
                        v-decorator="[
                        'subject',
                        {
                            rules: [
                                { required: true, message: '请输入证书主题' },
                            ],
                            getValueFromEvent: event => {
                                return this.verify.formalSpace(event)
                            }
                        }
                    ]"
                />
            </a-form-item>-->
        </a-form>
        <div slot="footer">
            <a-button type="default" @click="cancelApply('sysUserDtos')">取消</a-button>
            <a-button type="primary" @click="saveApply">确定</a-button>
        </div>
    </a-modal>
</template>

<script>
import verify from 'libs/verify'

export default {
    name: 'addEdit',
    data() {
        return {
            form: this.$form.createForm(this),
            verify,
            showAddTable: false,
            roleType: '',
            flags: '',
            countOrgChange: 0,
            originalData: {},
            selectedNode: {},
            /*选中的记录*/
            checkData: [],
            orgTreeData: [],
            subModeList: [{label: '逗号分割', value: '0'}, {label: '斜线分割', value: '1'} ],
        }
    },
    computed: {
        formStyle() {
            return this.$store.state.common.formStyle
        },
    },
    methods: {

        /**
         * Function    : change
         * Description :
         * Input       : val
         * Return      : none
         */
        change(val) {
            let _this = this
            setTimeout(function() {
                if (val) {
                    _this.$refs['refocus'].focus()
                }
            }, 200)
        },

        /**
         * Function    : showModal
         * Description : 打开弹框
         * Input       : none
         * Return      : none
         */
        showModal() {
            this.showAddTable = true
        },

        /**
         * Function    : saveApply
         * Description : 用户信息新增、编辑保存
         * Input       : name
         * Return      : none
         */
        saveApply(e) {
            e.preventDefault()
            let _this =this
            this.form.validateFields((err, value) => {
                if (!err) {
                  _this.$otherGetParams ('initSystem', {deviceId:value.deviceSerialNumber}, 'qrngser').then (res=> {
                    if (res.code === 0) {
                      _this.showAddTable = false
                      //需要重新登录
                      this.$store.dispatch ('logoutAct').then (response => {
                        if (response.code === 0) {
                          _this.$store.commit ('clearOpenedSubmenu')
                          _this.$store.commit ('logout', this)
                          _this.$message.success ('出厂设置成功，请重新登录！')
                          _this.$emit('getSystemSrvNameActt')
                        } else {
                          this.$message.error (response.message)
                        }
                      })
                    } else {
                      _this.$message.error (res.message)
                    }
                  }).catch (e => {
                    this.$message.error ('后台系统异常')
                  })
                }
            })
        },

        /**
         * Function    : selectedTreeId
         * Description :
         * Input       : val
         * Return      : none
         */
        selectedTreeId(val) {
            this.$refs['orgId'].resetField()
            this.sysUserDtos.orgId = val.id

            /**
             * 级联查询组织机构下的角色
             */
            /*if (this.countOrgChange > 0) {
          this.$store.dispatch('loadOrgsRolesAct',{orgId : val.id}).then(response => {
            this.roleOpts = response;
            this.sysUserDtos.roleId = '';
          });
        }*/
            this.countOrgChange = 1
        },

        /**
         * Function    : cancelApply
         * Description : 取消弹出框
         * Input       : name
         * Return      : none
         */
        cancelApply() {
            this.showAddTable = false
            let _this = this
            setTimeout(function() {
                _this.form.resetFields()
                _this.roleOpts = []
            }, 100)
        },

        /**
         * Function    : resetUserPasswd
         * Description : 重置用户密码
         * Input       : none
         * Return      : none
         */
        resetUserPasswd(name) {
            this.$store.dispatch('resetUserPasswdAct', { userName: this.sysUserDtos.userName }).then(response => {
                if (response.code === 0) {
                    this.$message.success(response.message)
                    this.showAddTable = false
                    this.roleOpts = []
                    this.$refs[name].resetFields()
                } else {
                    this.$message.error(response.message)
                }
            })
        }
    },

    created() {}
}
</script>
